package com.zara.store.client.clientmodel;

import java.math.BigDecimal;

import com.zara.store.client.model.StoreProxy;
import com.zara.store.common.InvoiceItemVO;
import com.zara.store.common.InvoiceVO;

public class RetailInvoice {
	
	private StoreProxy sp;
	private long number;
	private Basket basket;
	private String clientName ="";
	private String clientAddress ="";
	private String clientCUIT ="Consumidor Final";
	private String clientStatusIVA ="INSCRIPTO";
	private BigDecimal taxIVA = new BigDecimal(".21");
	private String invoiceType ="B";
	private InvoiceVO invoiceVO;

	public RetailInvoice(StoreProxy proxy) {
		this.sp = proxy;
	}

	public void setBasket(Basket basket) {
		this.basket = basket;
	}

	public Basket getBasket() {
		return basket;
	}

	public long getNumber() {
		return number;
	}

	public void setNumber(long number) {
		this.number = number;
	}

	public void clearNumber() {
		setNumber(0);
		setInvoiceVO(null);
	}

	public void createInvoice() {
//		BigDecimal taxIVA = BigDecimal.ZERO;
		BigDecimal total = basket.getTotal();
		BigDecimal subTotal = basket.getTotal();
		InvoiceVO invoiceVO = new InvoiceVO(
				invoiceType, total, subTotal, taxIVA, clientStatusIVA, 
				clientName, clientCUIT, clientAddress);
		for(BasketLine l: basket.getElements()) {
			invoiceVO.addLine(new InvoiceItemVO(
					l.getProductId(),l.getAmount(),l.getPrice(),l.getLineTotal()));
		}
		invoiceVO = sp.createInvoice(invoiceVO);
		setInvoiceVO(invoiceVO);
		setNumber(invoiceVO.getId());
	}

	private void setInvoiceVO(InvoiceVO invoiceVO) {
		this.invoiceVO = invoiceVO;
	}

	public void setClientAddress(String value) {
		this.clientAddress = value;
	}

	public void setInvoiceType(String value) {
		this.invoiceType = value;
	}

	public void setInvoiceTaxIVA(BigDecimal value) {
		this.taxIVA = value;
	}

	public void setClientStatusIVA(String value) {
		this.clientStatusIVA = value;
	}

	public void setClientName(String value) {
		this.clientName = value;
	}

	public void setClientCUIT(String value) {
		this.clientCUIT = value;
	}

	public InvoiceVO getInvoiceVO() {
		return invoiceVO;
	}
	
	public boolean hasInvoiceVO() {
		return invoiceVO != null;
	}

	public String getInvoiceType() {
		return invoiceType;
	}

}
